<template>
  <div class="app-container">
    <div style="display: flex;height: 100%">
      <div class="div-big" style="flex: 1;height: 100%;">
        <div class="mb10" style="border-bottom: 1px #D0D0D066 dotted;padding-bottom: 5px">
          <span style="font-size: 14px">患者列表</span>
          <el-tooltip
            class="box-item"
            content="患者录入"
            effect="dark"
            placement="top"
          >
            <el-button style="float: right" type="primary" @click="patientEntry">
              <template #icon>
                <i-ep-plus/>
              </template>
            </el-button>
          </el-tooltip>
          <base-dept-tree
            :org-id="userInfo.orgId"
            :placeholder="'开单科室'"
            :value="form.deptId"
            class="mt10"
            @deptSelect="deptSelect"
          />
          <el-row class="mt10" style="margin-left: -10px;">
            <el-radio-group
              v-model="form.isRegister"
              class="mb5"
              style="width: 100%"
              @change="isRegisterChange"
            >
              <el-radio-button
                key="1"
                :border="true"
                value="N"
              >未登记患者
              </el-radio-button>
              <el-radio-button
                key="0"
                :border="true"
                value="Y"
              >已登记患者
              </el-radio-button>
            </el-radio-group>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-input
                v-model="form.itemInfo"
                class="mb5 left-patient"
                clearable
                placeholder="姓名、门诊号"
                @input="refreshInfiniteList"
              >
                <template #append>
                  <el-tooltip content="刷新列表" effect="dark" placement="bottom">
                    <el-button
                      style="float: right"
                      type="primary"
                      @click="refreshInfiniteList"
                    >
                      <template #icon>
                        <i-ep-search/>
                      </template>
                    </el-button>
                  </el-tooltip>
                </template>
              </el-input>
            </el-col>
            <el-col :span="12"></el-col>
          </el-row>
        </div>
        <div class="infinite-list-wrapper" style="overflow-y: auto">
          <div
            v-infinite-scroll="load"
            :infinite-scroll-disabled="disabled"
          >
            <div v-for="(patient, index) in patientList"
                 :id="'patientCard-' + index"
                 :key="patient.id"
                 class="list-item box-card left-small-card" @click.stop="patientClick(patient, index)">
              <el-row class="patient-row">
                <el-col :span="11" class="ml10" style="text-align: left;"><span class="fz15" style="color:rgb(1 233 145);">{{
                    patient.name
                  }}</span></el-col>
                <el-col :span="8"><span class="fz13">{{ patient.ageAll }}</span></el-col>
                <el-col :span="2" style="margin-top: 3px;">
                  <svg-icon
                    :icon-class="
                      dictStore.getMateDictInfo(dictDataMap['sys_user_sex'], patient.sex)
                        .dictValue === '1'
                        ? 'man'
                        : 'woman'
                    "
                  />
                </el-col>
                <el-col :span="22" class="ml10" style="text-align: left;">
                  <span class="fz13">门诊号: {{ patient.clinicNo }}</span>
                </el-col>
                <el-col :span="22" class="ml10" style="text-align: left;">
                  <span class="fz13">接诊医生: {{ patient.doctorName }}</span>
                </el-col>
                <el-col :span="22" class="ml10" style="text-align: left;">
                  <span class="fz13">接诊科室: {{ patient.deptName }}</span>
                </el-col>
              </el-row>
            </div>
          </div>
          <p v-if="loading && patientList.length > 0" class="div-p">加载中...</p>
          <p v-if="noMore || patientList.length == 0" class="div-p"> 没有更多数据了</p>
        </div>
      </div>
      <div class="ml10 div-big" style="flex: 8;height: 100%;">
        <template v-if="!patientInfo">
          <el-empty :image-size="200" description="请选择左侧患者"/>
        </template>
        <template v-else>
          <div class="search-container">
            <el-descriptions
              :column="5"
              border
              class="mb5"
              title="患者信息"
            >
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    姓名
                  </div>
                </template>
                <span class="ys-css">{{ patientInfo.name }}</span>
                <span>{{ patientInfo.ageAll }}</span>
                <svg-icon :icon-class="
                      dictStore.getMateDictInfo(dictDataMap['sys_user_sex'], patientInfo.sex)
                        .dictValue === '1'
                        ? 'man'
                        : 'woman'
                    "
                          class="ml10"
                />

              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    联系电话
                  </div>
                </template>
                {{ patientInfo.phone }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    开单科室
                  </div>
                </template>
                {{ patientInfo.deptName }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    开单人
                  </div>
                </template>
                {{ patientInfo.doctorName }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    就诊日期
                  </div>
                </template>
                {{ patientInfo.visitDate }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    住址
                  </div>
                </template>
                {{ patientInfo.address }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    诊断
                  </div>
                </template>
                {{ patientInfo.diagnosisStr ? patientInfo.diagnosisStr : '无' }}
              </el-descriptions-item>
            </el-descriptions>
          </div>
          <el-row :gutter="10" class="mb8">
            <template v-if="form.isRegister === 'N'">
              <el-col :span="1.5">
                <el-button plain
                           type="primary" @click="clearGroupNumber">
                  <template #icon>
                    <i-ep-delete/>
                  </template>
                  清除组号
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button plain type="success"
                           @click="submitForm">
                  <template #icon>
                    <svg-icon icon-class="save"/>
                  </template>
                  登记
                </el-button>
              </el-col>
            </template>
            <template v-if="form.isRegister === 'Y'">
              <el-col :span="1.5">
                <el-button plain type="danger"
                           @click="infusionRecordVoucherPrint">
                  <template #icon>
                    <i-ep-printer/>
                  </template>
                  输液记录凭证打印
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button plain type="warning"
                           @click="infusionCardPrint">
                  <template #icon>
                    <i-ep-printer/>
                  </template>
                  输液卡打印
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button plain type="info"
                           @click="infusionLabelPrint">
                  <template #icon>
                    <i-ep-printer/>
                  </template>
                  输液瓶签打印
                </el-button>
              </el-col>
            </template>

          </el-row>
          <el-row>
            <el-col v-if="form.isRegister === 'N'" :span="5">
              <el-table ref="prescriptionApplyListRef" v-adaptive="20" :border="true" :data="prescriptionApplyList"
                        :header-cell-style="$headerCellStyle" :stripe="true"
                        highlight-current-row @selection-change="handleSelectionChange">
                <el-table-column :align="'center'" type="selection" width="40"/>
                <el-table-column :align="'center'" label="申请单号" prop="applyNo">
                  <template #default="scope">
                    <span class="ys-css">{{ scope.row.applyNo }}</span>
                  </template>
                </el-table-column>
                <el-table-column :align="'center'" label="开单时间" prop="createTime" width="140">
                  <template #default="scope">
                    <span>{{ $parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
            <el-col :span="form.isRegister === 'N' ? 19 : 24">
              <template v-if="applyIds.length> 0 || patientInfo.infusionRegisterId">
                <el-form ref="prescriptionFormRef" :model="prescriptionForm" :rules="rules">
                  <el-table ref="prescriptionItemListRef" :border="true"
                            :data="prescriptionForm.prescriptionItemList" :header-cell-style="$headerCellStyle"
                            :stripe="true"
                            highlight-current-row>
                    <el-table-column :align="'center'" label="组号" prop="groupNumberShow" width="90">
                      <template #default="scope">
                        <el-form-item :prop="'prescriptionItemList.' + scope.$index + '.groupNumberShow'"
                                      :rules="rules.groupNumberShow" class="table-form-margin">
                          <el-input v-model="scope.row.groupNumberShow"
                                    v-keyupEnter="{ index: scope.$index, name: 'groupNumber' }"
                                    :disabled="form.isRegister === 'Y'" class="groupNumberShow"
                                    clearable
                                    type="number"></el-input>
                        </el-form-item>
                      </template>
                    </el-table-column>
                    <el-table-column :align="'left'" label="药品" prop="drugName">
                      <template #default="scope">
                      <span>{{ scope.row.groupNumberStyle }}<span class="ml5 ys-css">{{
                          scope.row.drugName
                        }}</span></span>
                      </template>
                    </el-table-column>
                    <el-table-column :align="'center'" :show-overflow-tooltip="true" label="规格" prop="packageSpec"
                                     width="140"/>
                    <el-table-column :align="'center'" label="途径" prop="channel" width="100">
                      <template #default="scope">
                        <span>{{ scope.row.channelName }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="单次用量" prop="singleDosage" width="110">
                      <template #default="scope">
                          <span style="margin-left: -15px"> {{ scope.row.singleDosage }}<dict-data-tag
                            :options="dictDataMap['dose_unit']" :value="scope.row.singleUnit"
                            style="margin: -23px -35px 0px 35px"/></span>
                      </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="频次" prop="performFreq" width="100">
                      <template #default="scope">
                        <span>{{ scope.row.performFreqName }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="天数(天)" prop="abidance" width="70">
                      <template #default="scope">
                        <span>{{ scope.row.abidance }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="数量" prop="num" width="110">
                      <template #default="scope">
                          <span>{{ scope.row.num }} <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packSize === '1' ?
                            scope.row.minUnits : scope.row.packageUnits" style="margin: -23px -45px 0px 27px"/>
                          </span>
                      </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="免皮试" prop="noSkin" width="60">
                      <template #default="scope">
                          <span>
                            <dict-data-tag :options="dictDataMap['sys_yes_no']" :value="scope.row.noSkin"/>
                          </span>
                      </template>
                    </el-table-column>
                    <el-table-column v-if="form.isRegister === 'Y'" :header-align="'center'"
                                     class-name="small-padding fixed-width"
                                     fixed="right" label="操作" width="120">
                      <template #default="scope">
                        <el-button class="mb10 ml10" plain type="warning" @click="startPriming(scope.row)">
                          <svg-icon class="mr5" icon-class="save"/>
                          开始配液
                        </el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-form>
              </template>
              <el-empty v-else :image-size="200" description="请勾选左侧处方"/>
            </el-col>
          </el-row>
        </template>
      </div>
    </div>

    <!-- 开始配液弹出框 -->
    <el-dialog v-model="open" :close-on-click-modal="false" append-to-body title="配液" width="55%">
      <el-row class="patient-row">
        <el-col :span="3" class="ml10" style="text-align: left;"><span class="fz15" style="color: #7171ff;">{{
            patientInfo.name
          }}</span></el-col>
        <el-col :span="2"><span class="fz13">{{ patientInfo.ageAll }}</span></el-col>
        <el-col :span="1" style="margin-top: 3px;">
          <svg-icon
            :icon-class="
                      dictStore.getMateDictInfo(dictDataMap['sys_user_sex'], patientInfo.sex)
                        .dictValue === '1'
                        ? 'man'
                        : 'woman'
                    "
          />
        </el-col>
        <el-col :span="5" class="ml10" style="text-align: left;">
          <span class="fz13">门诊号: {{ patientInfo.clinicNo }}</span>
        </el-col>
        <el-col :span="5" class="ml10" style="text-align: left;">
          <span class="fz13">接诊医生: {{ patientInfo.doctorName }}</span>
        </el-col>
        <el-col :span="6" class="ml10" style="text-align: left;">
          <span class="fz13">接诊科室: {{ patientInfo.deptName }}</span>
        </el-col>
      </el-row>
      <el-row class="mt5">
        <span class="ml10 mr20">药品名称：<span class="ys-css">{{ tableRow.drugName }}</span></span>
        <span class="ml10 mr20">规格：<span class="ys-css">{{ tableRow.packageSpec }}</span></span>
        <span class="ml10 mr20">剂量：<span class="ys-css"> {{ tableRow.singleDosage }} <dict-data-tag
          :options="dictDataMap['dose_unit']" :value="tableRow.singleUnit"
          style="margin: -20px 0 0 100px"/></span></span>
        <span class="ml10 mr20">频次：<span class="ys-css">{{ tableRow.performFreqName }}</span></span>
        <span class="ml10 mr20">天数：<span class="ys-css">{{ tableRow.abidance }}天</span></span>
        <span class="ml10 mr20">总数量：<span class="ys-css">
          {{ tableRow.num }} <dict-data-tag :options="dictDataMap['spec_unit']" :value="tableRow.packSize === '1' ?
                            tableRow.minUnits : tableRow.packageUnits" style="margin: -20px -45px 0 120px"/>
        </span></span>
      </el-row>
      <el-row>
        <!--        <template v-for="index in Number(tableRow.infusionNumber)" :key="index">-->
        <template v-for="(item,index) in infusionRecordList" :key="index">
          <el-col :span="24" class="mb10" style="border: 1px solid #c4c4c4;">
            <span style="margin-left: 100px">序号：{{ Number(index) + 1 }}</span>
            <el-steps :active="item.operatorType" align-center finish-status="success"
                      process-status="finish">
              <el-step title="配液">
                <template #description>
                      <span v-if="item.distributionTime">
                        <div>操作人：{{ item.distributionNurseName }}</div>
                        <div>操作时间：{{ item.distributionTime }}</div>
                      </span>
                  <span v-else>
                    <el-popconfirm title="确定要录入吗？">
                      <template #reference>
                       <el-link class="mb5" type="primary">点击录入【配液】信息</el-link>
                      </template>
                      <template #actions="{ confirm, cancel }">
                        <el-button @click="cancel">取消</el-button>
                        <el-button
                          type="primary"
                          @click="stepsClick1('1',item)"
                        >
                          确定
                        </el-button>
                      </template>
                    </el-popconfirm>
                      </span>
                </template>
              </el-step>
              <el-step title="穿刺">
                <template #description>
                      <span v-if="item.punctureTime">
                        <div>操作人：{{ item.punctureNurseName }}</div>
                        <div>操作时间：{{ item.punctureTime }}</div>
                      </span>
                  <span v-else>
                    <el-popconfirm title="确定要录入吗？">
                      <template #reference>
                       <el-link class="mb5" type="primary">点击录入【穿刺】信息</el-link>
                      </template>
                      <template #actions="{ confirm, cancel }">
                        <el-button @click="cancel">取消</el-button>
                        <el-button
                          type="primary"
                          @click="stepsClick2('2',item)"
                        >
                          确定
                        </el-button>
                      </template>
                    </el-popconfirm>
                      </span>
                </template>
              </el-step>

              <el-step title="换液拔针">
                <template #description>
                      <span v-if="item.pullNeedleTime">
                        <div>操作人：{{ item.pullNeedleNurseName }}</div>
                        <div>操作时间：{{ item.pullNeedleTime }}</div>
                      </span>
                  <span v-else>
                    <el-popconfirm title="确定要录入吗？">
                      <template #reference>
                       <el-link class="mb5" type="primary">点击录入【换液拔针】信息</el-link>
                      </template>
                      <template #actions="{ confirm, cancel }">
                        <el-button @click="cancel">取消</el-button>
                        <el-button
                          type="primary"
                          @click="stepsClick3('3',item)"
                        >
                          确定
                        </el-button>
                      </template>
                    </el-popconfirm>
                      </span>
                </template>
              </el-step>
            </el-steps>
          </el-col>
        </template>
      </el-row>
    </el-dialog>

    <!-- 操作信息录入弹出框 -->
    <el-dialog v-model="infoOpen" :close-on-click-modal="false" :title="infoTitle" append-to-body width="15%">
      <el-row>
        <el-col :span="24" class="ml10 mb8" style="margin-top: -10px;color: #0463c4">
          <el-checkbox v-model="isSameGroupCheck" label="同一组中的其他液体是否同时录入"/>
        </el-col>
        <el-col :span="24">
          <el-form-item label="操作时间" prop="operatorDate" style="width: 100%">
            <el-date-picker v-model="operatorDate" placeholder="请选择操作时间"
                            style="width: 100%" type="datetime"
                            value-format="YYYY-MM-DD HH:mm:ss">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="操&nbsp;&nbsp;作&nbsp;人" prop="startDate" style="width: 100%">
            <el-select
              v-model="operatorUserid"
              placeholder="请选择操作人"
              style="width: 100%"
            >
              <el-option
                v-for="user in operatorUserList"
                :key="user.userId"
                :label="user.nickName"
                :value="user.userId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="infoOpenSubmitForm">确 定</el-button>
          <el-button @click="infoOpenCancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {getInfusionRegisterStayItemList, getInfusionRegisterStayList} from "@/api/infusion/infusionRegisterStay.ts"
import {
  addInfusionRegister,
  listInfusionRegisterAndPrescItem,
  listInfusionRegisterInfoList
} from "@/api/infusion/infusionRegister.ts"
import {getDeptVsUserInfo} from "@/api/system/dept.ts"
import {
  addInfusionRecordBatch,
  listInfusionRecord,
  updateInfusionRecordByDialog
} from "@/api/infusion/infusionRecord.ts"
import {listPrescriptionApplyByInfusion} from "@/api/patient/prescriptionApply.ts"
import {useUserStoreHook} from "@/store/modules/user.ts";
import {useDictStoreHook} from "@/store/modules/dict.ts";
import {positiveNumberNotNull} from "@/utils/myRules.ts";

const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo
const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData("sys_user_sex", "infusion_type_dict", "dose_unit", "spec_unit", "sys_yes_no"));
const {proxy} = getCurrentInstance() as any
const currentSelectDept = proxy.$currentSelectDept

const applyIds = ref([]);
const selectionThis = ref<any>([]);
const single = ref(true);
const multiple = ref(true);

// 配液弹出框
const open = ref(false);
// 配液弹出框 当前点击的操作类型
const currentType = ref("")
const operatorItem = ref<any>({})
const operatorUserList = ref<any>([])
const operatorUserid = ref(userInfo.userId)
const operatorDate = ref(proxy.$parseTime(new Date(), "{y}-{m}-{d} {h}:{i}:{s}"))
// 信息录入弹出框
const infoOpen = ref(false);
const infoTitle = ref("配液信息录入")

const infusionRecordList = ref<any>([])
// 同组液体是否同时录入
const isSameGroupCheck = ref(true)
const prescriptionFormRef = ref()
const prescriptionItemListRef = ref()
const prescriptionApplyListRef = ref()
// 输液类型
const infusionType = ref("1")

const infusionTypeList = dictDataMap.value['infusion_type_dict']
const patientList = ref<any>([])
// 处方明细列表单行数据
const tableRow = ref<any>({})
const count = ref(1)
const loading = ref(false)
const noMore = computed(() => count.value === patientList.value.length)
const disabled = computed(() => loading.value || noMore.value)
const form = ref<any>({
  pageNum: 1 as number,
  pageSize: 5,
  orgId: userInfo.orgId,
  isRegister: 'N',
  deptId: "",
  itemInfo: ""
})
// 药品明细列表
const prescriptionForm = ref<any>({
  prescriptionItemList: []
})
const patientInfo = ref<any>({})
const prescriptionApplyList = ref<any>([])
const load = () => {
  loading.value = true
  if (!form.value.deptId) {
    return ElMessage.error("请先选择开单科室")
  }
  if (form.value.isRegister === "Y") {
    // 已登记
    listInfusionRegisterInfoList(form.value).then((res: any) => {
      if (res.rows.length > 0) {
        count.value = res.total
        patientList.value = patientList.value.concat(res.rows)
        form.value.pageNum += 1
        loading.value = false
      }
    })
  } else {
    getInfusionRegisterStayList(form.value).then((res: any) => {
      if (res.rows.length > 0) {
        count.value = res.total
        patientList.value = patientList.value.concat(res.rows)
        form.value.pageNum += 1
        loading.value = false
      }
    })
  }
}

// 表单校验
const rules = ref({
  groupNumberShow: [
    {required: true, validator: positiveNumberNotNull, trigger: "blur"}
  ]
})


function isRegisterChange() {
  patientList.value = []
  patientInfo.value = undefined
  form.value.pageNum = 1
  load()
}

function patientEntry() {

}

function deptSelect(node: any) {
  if (!node) {
    form.value.deptId = "";
  } else {
    form.value.deptId = node.deptId;
  }
  patientList.value = []
  form.value.pageNum = 1
  load()
}

function refreshInfiniteList() {
  isRegisterChange()
}

/** 左侧患者列表选中事件 */
function patientClick(item, index) {
  proxy.$patientClickCssChange(index, patientList.value);
  patientInfo.value = item
  const params = {
    orgId: form.value.orgId,
    clinicId: item.id,
    infusionRegisterId: item.infusionRegisterId
  }
  if (form.value.isRegister === "Y") {
    listInfusionRegisterAndPrescItem(params).then(res => {
      prescriptionForm.value.prescriptionItemList = []
      res.data.forEach((item: any) => {
        item.singleUnitName = dictStore.getMateDictInfo(dictDataMap.value['dose_unit'], item.singleUnit).dictLabel
        const unit = item.packSize === "1" ? item.minUnits : item.packageUnits
        item.unitName = dictStore.getMateDictInfo(dictDataMap.value['spec_unit'], unit).dictLabel
        prescriptionForm.value.prescriptionItemList.push(item)
      })
    })
  } else {
    listPrescriptionApplyByInfusion(params).then(res => {
      prescriptionApplyList.value = res.data
      prescriptionApplyListRef.value.toggleAllSelection();
    })
  }
}

/**
 * 输液类型点击事件
 */
function handleClick(obj) {
  const objThis = toRaw(obj)
  infusionType.value = objThis.props.name
  getInfusionRegisterStayItemListFun()
}

/**
 * 处方列表勾选事件
 */
function getInfusionRegisterStayItemListFun() {
  if (applyIds.value.length <= 0) {
    return prescriptionForm.value.prescriptionItemList = []
  }
  const params = {
    orgId: form.value.orgId,
    // infusionType: infusionType.value,
    applyIds: applyIds.value
  }
  getInfusionRegisterStayItemList(params).then((res: any) => {
    prescriptionForm.value.prescriptionItemList = []
    res.data.forEach((item: any) => {
      item.singleUnitName = dictStore.getMateDictInfo(dictDataMap.value['dose_unit'], item.singleUnit).dictLabel
      const unit = item.packSize === "1" ? item.minUnits : item.packageUnits
      item.unitName = dictStore.getMateDictInfo(dictDataMap.value['spec_unit'], unit).dictLabel
      prescriptionForm.value.prescriptionItemList.push(item)
    })
  })
}

// 多选框选中数据
function handleSelectionChange(selection) {
  selectionThis.value = selection
  applyIds.value = selection.map(item => item.applyId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
  getInfusionRegisterStayItemListFun()
}

/**
 * 清除组号
 */
function clearGroupNumber() {
  prescriptionForm.value.prescriptionItemList.forEach(item => {
    item.groupNumberShow = ''
  })
}

/**
 * 登记
 */
function submitForm() {
  (prescriptionFormRef.value as any).validate(valid => {
    if (valid) {
      const params = {
        orgId: form.value.orgId,
        clinicId: patientInfo.value.id,
        applyIds: applyIds.value,
        infusionPrescriptionVos: prescriptionForm.value.prescriptionItemList,
        isAll: applyIds.value.length === prescriptionApplyList.value.length ? "Y" : "N"
      }
      ElMessageBox.confirm('是否确定进行登记？',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        addInfusionRegister(params).then(() => {
          ElMessage.success("登记成功")
        })
      }).catch(() => {
        ElMessage.info("操作终止")
      })

    }
  })
}

/**
 * 开始配液
 * @param row
 */
function startPriming(row: any) {
  isSameGroupCheck.value = true
  tableRow.value = row
  const params = {
    registerId: tableRow.value.registerId,
    groupNumber: tableRow.value.groupNumber,
    infusionItemId: tableRow.value.infusionItemId,
  }
  addInfusionRecordBatch(params).then(() => {
    listInfusionRecord({itemId: params.infusionItemId}).then((res: resType) => {
      infusionRecordList.value = res.data
      open.value = true
    })
  })
}

/**
 * 配液
 * @param type
 * @param item
 */
function stepsClick1(type, item) {
  if (item.operatorType === '1') {
    return ElMessage.error("液体已经进行了【配液】，不能再次操作")
  }
  if (item.operatorType === '2') {
    return ElMessage.error("液体已经进行【穿刺】，不能操作")
  }
  if (item.operatorType === '3') {
    return ElMessage.error("液体已经进行【换液拔针】，不能操作")
  }
  infoOpen.value = true
  infoTitle.value = "配液信息录入"
  operatorDate.value = proxy.$parseTime(new Date(), "{y}-{m}-{d} {h}:{i}:{s}")
  operatorItem.value = item
  currentType.value = type
  getDeptVsUserInfoFun()
}

/**
 * 穿刺
 * @param type
 * @param item
 */
function stepsClick2(type, item) {
  if (!item.operatorType) {
    return ElMessage.error("液体还没有进行【配液】，不能操作")
  }
  if (item.operatorType === '2') {
    return ElMessage.error("液体已经进行了【穿刺】，不能再次操作")
  }
  if (item.operatorType === '3') {
    return ElMessage.error("液体已经进行【换液拔针】，不能操作")
  }
  infoOpen.value = true
  infoTitle.value = "穿刺信息录入"
  operatorDate.value = proxy.$parseTime(new Date(), "{y}-{m}-{d} {h}:{i}:{s}")
  operatorItem.value = item
  currentType.value = type
  getDeptVsUserInfoFun()
}

/**
 * 换药拔针
 * @param type
 * @param item
 */
function stepsClick3(type, item) {
  if (!item.operatorType) {
    return ElMessage.error("液体还没有进行【配液】，不能操作")
  }
  if (item.operatorType === '1') {
    return ElMessage.error("液体还没有进行【穿刺】，不能操作")
  }
  if (item.operatorType === '3') {
    return ElMessage.error("液体已经进行了【换药拔针】，不能操作")
  }
  infoOpen.value = true
  infoTitle.value = "换液拔针信息录入"
  operatorDate.value = proxy.$parseTime(new Date(), "{y}-{m}-{d} {h}:{i}:{s}")
  operatorItem.value = item
  currentType.value = type
  getDeptVsUserInfoFun()
}

/**
 * 通过科室id获取本科室下所有存在的用户
 */
function getDeptVsUserInfoFun() {
  const params = {
    deptId: currentSelectDept,
  }
  getDeptVsUserInfo(params).then((res: resType) => {
    operatorUserList.value = res.data
  })
}

function infoOpenCancel() {
  infoOpen.value = false
}

/**
 * 操作弹出框提交保存事件
 */
function infoOpenSubmitForm() {
  let data: any = {
    operatorType: currentType.value,
    isSameGroupCheck: isSameGroupCheck.value ? "Y" : "N",
    infusionSerialNumber: operatorItem.value.infusionSerialNumber,
    infusionRecordId: operatorItem.value.infusionRecordId,
    itemId: operatorItem.value.itemId,
  }
  if (currentType.value === '1') {
    // 配液
    data.distributionTime = operatorDate.value
    data.distributionNurse = operatorUserid.value
  } else if (currentType.value === '2') {
    // 穿刺
    data.punctureTime = operatorDate.value
    data.punctureNurse = operatorUserid.value
  } else if (currentType.value === '3') {
    //换药拔针
    data.pullNeedleTime = operatorDate.value
    data.pullNeedleNurse = operatorUserid.value
  }
  updateInfusionRecordByDialog(data).then(()=>{
    listInfusionRecord({itemId: operatorItem.value.itemId}).then((res: resType) => {
      infusionRecordList.value = res.data
    })
    infoOpenCancel()
    ElMessage.success("操作成功")
  })
}
</script>

<style scoped>
.infinite-list-wrapper {
  height: calc(100% - 140px);
}

.infinite-list-wrapper .list {
  padding: 0;
  margin: 0;
}

.infinite-list-wrapper .list-item {
  height: 82px;
  background-color: rgba(255, 255, 255, 0.4);
  border: 1px solid #027687;
  border-radius: 5px;
}

.div-p {
  color: #4ba2f0;
  font-size: 15px;
  justify-content: center;
  display: flex;
  margin: 8px 0 8px 0
}

.div-big {
  border: 1px solid #027687;
  min-width: 240px;
  border-radius: 5px;
  padding: 5px;
}

:deep(.el-tabs__content) {
  padding: 1px;
}

:deep(.el-step__title.is-success) {
  color: #087712;
}

:deep(.el-step__title.is-wait) {
  color: #d4040f;
}

</style>
